/**
 * Author: LiangYong
 * Created on 2008-1-11
 */

package com.yyq.sos.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;

import com.yyq.sos.dao.SysLogsDao;
import com.yyq.sos.dao.SysLogsDaoImpl;
import com.yyq.sos.pojo.SysLogs;
import com.yyq.sos.util.page.PageFormData;

public class SysLogsBpoImpl extends BaseBpoImpl implements SysLogsBpo {

	private SysLogsDao dao;
	
	@Resource(name="sysLogsDao")
	public void setDao(SysLogsDao dao) {
		this.dao = dao;
		this.setBaseDao(dao);
	}

	//select a.log_id,b.user_name,b.user_cname,a.table_name,a.action_type,a.begin_time,a.action_result 
	public PageFormData searchSqlPage(String sql, int page, int size) {
		PageFormData pfd = super.searchSqlPage(sql, page, size);
		List list = pfd.getData();
		List data = new ArrayList();
		for(int i=0; i<list.size(); ++i){
			SysLogs log = new SysLogs();
			Object[] vo = (Object[])list.get(i);
			log.setId(new Long(String.valueOf(vo[0])));
			log.setUserName(String.valueOf(vo[1]));
			log.setUserCname(String.valueOf(vo[2]));
			log.setTableName(String.valueOf(vo[3]));
			log.setActionType(String.valueOf(vo[4]));
			log.setBeginTime((java.util.Date)(vo[5]));
			log.setActionResult(String.valueOf(vo[6]));
			data.add(log);
		}
		pfd.setData(data);
		return pfd; 
	}
	
	public  static void main(String[] sss)throws Exception{
		new SysLogsBpoImpl().backup("20081010", "20081010");
	}
	public boolean backup(String beginDate,String endDate){
		boolean bool = false;
		Session session = null;
		Connection conn = null;
		try{
			session = dao.getSessionFactory().openSession();
			conn = session.connection();
			conn.setAutoCommit(false);
			PreparedStatement ps = conn.prepareStatement("select * from ss_sys_logs where begin_time between ? and ?");
			ps.setTimestamp(1, Timestamp.valueOf(beginDate.substring(0,4)+"-"+beginDate.substring(5,6)+"-"+beginDate.substring(7)+" 00:00:01"));
			ps.setTimestamp(2, Timestamp.valueOf(endDate.substring(0,4)+"-"+endDate.substring(5,6)+"-"+endDate.substring(7)+" 23:59:59"));
			PreparedStatement ps1 = conn.prepareStatement("insert into ss_sys_logs_h values(?, ?, ?, ?, ?, ?, ?, ?)");
			ResultSet rs = ps.executeQuery();
			int i = 1;
			List<Long> ids = new ArrayList<Long>();
			while(rs.next()){
				long id = rs.getLong(1);
				ids.add(id);
				ps1.setLong(1, id);
				ps1.setLong(2, rs.getLong(2));
				ps1.setString(3, rs.getString(3));
				ps1.setString(4, rs.getString(4));
				ps1.setString(5, rs.getString(5));
				ps1.setTimestamp(6, rs.getTimestamp(6));
				ps1.setBlob(7, rs.getBlob(7));
				ps1.setString(8, rs.getString(8));
				ps1.addBatch();
				if(((i++)%100)==0)
					ps1.executeBatch();
//				rs.deleteRow();
			}
			ps1.executeBatch();
			ps1.close();
			rs.close();
			ps.close();
			if(!ids.isEmpty()){
				ps = conn.prepareStatement("delete from ss_sys_logs where log_id = ?");
				i = 1;
				for(Long id : ids){
					ps.setLong(1, id);
					ps.addBatch();
					if(((i++)%1000)==0)
						ps.executeBatch();
				}
				ps.executeBatch();
			}
			bool = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(session != null){
					session.close();
				}
				conn.commit();
				conn.setAutoCommit(true);
				conn.close();
			} catch (SQLException e) {e.printStackTrace();}
		}
		return bool;
	}
	public boolean resume(String beginDate,String endDate){
		boolean bool = false;
		Session session = null;
		Connection conn = null;
		try{
			session = dao.getSessionFactory().openSession();
			conn = session.connection();
			PreparedStatement ps = conn.prepareStatement("select * from ss_sys_logs_h where begin_time between ? and ?",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			ps.setTimestamp(1, Timestamp.valueOf(beginDate.substring(0,4)+"-"+beginDate.substring(5,6)+"-"+beginDate.substring(7)+" 00:00:01"));
			ps.setTimestamp(2, Timestamp.valueOf(endDate.substring(0,4)+"-"+endDate.substring(5,6)+"-"+endDate.substring(7)+" 23:59:59"));
			PreparedStatement ps1 = conn.prepareStatement("insert into ss_sys_logs values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
			ResultSet rs = ps.executeQuery();
			int i = 1;
			while(rs.next()){
				ps1.setLong(1, rs.getLong(1));
				ps1.setString(2, rs.getString(2));
				ps1.setLong(3, rs.getLong(3));
				ps1.setString(4, rs.getString(4));
				ps1.setString(5, rs.getString(5));
				ps1.setString(6, rs.getString(6));
				ps1.setString(7, rs.getString(7));
				ps1.setTimestamp(8, rs.getTimestamp(8));
				ps1.setTimestamp(9, rs.getTimestamp(9));
				ps1.setString(10, rs.getString(10));
				ps1.setString(11, rs.getString(11));
				ps1.setString(12, rs.getString(12));
				ps1.addBatch();
				if(((i++)%100)==0)
					ps1.executeBatch();
				rs.deleteRow();
			}
			ps1.executeBatch();
			ps1.close();
			rs.close();
			ps.close();
			bool = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {}
			if(session != null){
				session.close();
			}
		}
		return bool;
	}
}
